/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Interpreter.Function.List;

import Interpreter.Function.Function;
import Interpreter.InterpreterVisitor;
import Interpreter.Observation;
import ast.AST;
import java.util.ArrayList;

/**
 *
 * @author Stephen
 */
public class SortTimeFunction extends Function {
    
    @Override
    public Object execute(ArrayList args) {
        ArrayList list1 = (ArrayList) ((AST)args.get(0)).accept(new InterpreterVisitor());
        
        boolean swapped = true;
        while (swapped) {
            swapped = false;
            for (int i = 0; i < list1.size() - 1; i ++) {
                Observation obj1 = (Observation) list1.get(i);
                Observation obj2 = (Observation) list1.get(i+1);
                if (obj2.getDate().before(obj1.getDate())) {
                    list1.remove(i+1);
                    list1.add(i, obj2);
                    swapped = true;
                }
            }
        }
        
        return list1;
    }
}
